package fr.ttalbot.flex.components
{
	import flash.events.MouseEvent;
	
	import fr.ttalbot.flex.effects.Arc;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.effects.Fade;
	import mx.effects.Parallel;
	import mx.events.EffectEvent;
	
	[Bindable]
	
	/**
	 * 
	 */
	public class Stack extends Canvas
	{
		private var __dataProvider: ArrayCollection;
		private var __openEventail: Parallel;
		private var __closeEventail: Parallel;
		private var __isOpen: Boolean;
		private var __direction: String;
		
		// ***************************************************************** //
		// ************************* CONSTRUCTOR *************************** //
		// ***************************************************************** //
		
		public function Stack() {
			super();
			createDataProvider();
			addEventListener(MouseEvent.CLICK, handle_mouseClick);
			__isOpen = false;
		}
		
		override protected function createChildren(): void {
			super.createChildren();
			direction = "down";
			__openEventail = new Parallel();
			__openEventail.addEventListener(EffectEvent.EFFECT_END, handle_eventailEnd);
			__closeEventail = new Parallel();
			__closeEventail.addEventListener(EffectEvent.EFFECT_END, handle_eventailEnd);
		}
		
		private function createDataProvider(): void {
			if (!__dataProvider)
				__dataProvider = new ArrayCollection();
		}
		
		private function updateStack(): void {
			var count: int = 1;
			for each (var item: * in dataProvider) {
				if (!rawChildren.contains(item)) {
					//var container: UIComponent = new UIComponent();
					//container.addChild(item);
					rawChildren.addChild(item);
					//createMove(container, count);
					createMove(item, count);
					count++;
				}
			}
		}
		
		private function createMove(item: *, count: int): void {
			var open: Arc = new Arc();
			var close: Arc = new Arc();
			var test: Fade
			var angle: int = 4;
			open.target = item;
			close.target = item;
			open.radius = 1000;
			open.angle = angle*count;
			close.radius = 150;
			open.duration = 200;
			close.duration = 200;
			open.yBy = count * this.height;
			close.yTo = 0;
			close.xTo = 0;
			__openEventail.children.push(open);
			__closeEventail.children.push(close);
		}
		
		
		// ***************************************************************** //
		// *************************** HANDLERS **************************** //
		// ***************************************************************** //
		
		private function handle_mouseClick(event: MouseEvent): void {
			removeEventListener(MouseEvent.CLICK, handle_mouseClick);
			if (!__isOpen)
				__openEventail.play();
			else
				__closeEventail.play();
		}
		
		private function handle_eventailEnd(event: EffectEvent): void {
			__isOpen = !__isOpen;
			addEventListener(MouseEvent.CLICK, handle_mouseClick);
		}
		
		
		// ***************************************************************** //
		// ********************** GETTERS / SETTERS ************************ //
		// ***************************************************************** //
		
		[Inspectable(type="Array")]
		public function get dataProvider(): ArrayCollection {
			return __dataProvider; 
		}
		public function set dataProvider(anArray: ArrayCollection): void {
			__dataProvider = anArray;
			updateStack();
			invalidateProperties();
		}
		
		[Inspectable(type="String", enumeration="up,down", defaultValue="down")]
		public function get direction(): String {
			return __direction;
		}
		public function set direction(aString: String): void {
			__direction = aString;
			invalidateProperties();
		}

	}
}